<template>
  <jm-form v-bind="searchFormConfig" v-model="formData">
    <template #header>
      <h1 class="header">高级检索页</h1>
    </template>
    <template #footer>
      <div class="footer">
        <el-button Plain @click="handleResetClick">重置</el-button>
        <el-button type="primary" @click="handleSearch">搜索</el-button>
      </div>
    </template>
  </jm-form>
</template>

<script lang="ts">
import { defineComponent, ref } from 'vue'

import JMForm from '@/base-ui/form/index'
import { IFormItem } from '@/base-ui/form/types'

export default defineComponent({
  props: {
    searchFormConfig: {
      type: Object,
      require: true
    }
  },
  components: { 'jm-form': JMForm },
  emits: ['resetBtnClick', 'queryBtnClick'],
  setup(props, { emit }) {
    // 动态配置 formData 的绑定值
    const formItems = props.searchFormConfig?.formItems ?? []
    const formOriginData: any = []
    formItems.forEach((item: IFormItem) => {
      formOriginData[item.field] = ''
    })
    const formData = ref(formOriginData)

    // 重置按钮
    const handleResetClick = () => {
      for (const key in formOriginData) {
        formData.value[key] = formOriginData[key]
      }
      emit('resetBtnClick')
    }

    // 搜索按钮
    const handleSearch = () => {
      emit('queryBtnClick', formData.value)
    }
    return {
      formData,
      handleResetClick,
      handleSearch
    }
  }
})
</script>

<style scoped>
.header {
  padding: 0;
  margin: 0 0 10px 0;
}
.footer {
  text-align: right;
  padding-bottom: 10px;
}
</style>
